package com.woniuxy.service.impl;

import com.woniuxy.dao.ManagerLoginDao;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.ManagerLoginService;
import com.woniuxy.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @Author：jojo
 * @Date：2022/3/10 11:13
 * @Description :TODO
 */
@Service
public class ManagerLoginServiceImpl implements ManagerLoginService {

    @Autowired(required = false)
    RedisTemplate redisTemplate;
    @Autowired(required = false)
    ManagerLoginDao managerLoginDao;
    @Override
    public ResponseEntity login(User user) {
        int num = managerLoginDao.login(user);
        if (num > 0) {
            //如果数据库有该用户的信息  则使用JWTUtil创建token
            String token = JWTUtil.sign(user.getAccount(), user.getPassword());
            redisTemplate.opsForValue().set(token,user);
            user.setToken(token);
            user.setId(num);
            return ResponseEntity.success("login success",user);
        } else {
            return ResponseEntity.error("login failed");
        }
    }
}
